package frsf.cidisi.exercise.tp2_ia.situationCalculus;

import frsf.cidisi.faia.agent.ActionFactory;
import frsf.cidisi.faia.agent.Perception;
import frsf.cidisi.faia.agent.situationcalculus.KnowledgeBase;
import frsf.cidisi.faia.agent.situationcalculus.SituationCalculusPerception;
import frsf.cidisi.faia.exceptions.PrologConnectorException;
import java.util.Hashtable;

public class EstadoAgente extends KnowledgeBase {

	public static int INITIAL= 0;
	public static int FORECAST_REQUEST_SENT = 1;
	public static int FORECAST_REQUEST_AGREE = 2;
	public static int FORECAST_REQUEST_REFUSE = 3;
	public static int INFORM_PLANNED_EVENTS_SENT = 4;
	public static int INFORM_POS_FORECAST_SENT = 5;
	public static int PROPOSE_DEMAND_FORECAST_1 = 6;
	public static int DEMAND_FORECAST_RESPONSE = 7;
	public static int ACCEPT_PROPOSAL_SENT = 8;
	public static int UPDATED_DEMAND_FORECAST_SENT = 9;
	public static int SUCCESS = 10;
	public static int PROPOSE_DEMAND_FORECAST_2 = 11;
	public static int SUBPROCESS_3_FINALIZED = 12;
	
	private int tpoTranscurrido;
	private int diasEsperando;
	private int posicionAgente;
	private int iteraciones; // iteraciones dentro del subproceso 3
	
	private String perceptionName;
	private int perceptionTime;
	    
    public EstadoAgente() throws PrologConnectorException {
		
        super("agente_logic.pl");

        this.initState();
    }

    @Override
    public ActionFactory getActionFactory() {
        return ComputersMarketActionFactory.getInstance();
    }

    @Override
    public String getSituationPredicate() {
        return "actualSituation";
    }

    @Override
    public String getBestActionPredicate() {
        return "bestAction";
    }

    @Override
    public String getExecutedActionPredicate() {
        return "executedAction";
    }

    @Override
    public void updateState(Perception p) {
        this.tell((SituationCalculusPerception) p);
    }

    @Override
    public void initState() {
    	
    	this.addKnowledge("position(initial,0)");
        this.addKnowledge("tiempo(0,0)");
        
		
    }

    @Override
    public String toString() {
        
        StringBuffer str = new StringBuffer();

        str.append("Posicion: ");
        str.append(this.getposicionAgente());
        str.append(" | Tiempo Transcurrido: ");
        str.append(this.gettpoTranscurrido());
        
        
        return str.toString();
        
    }
    
    public int gettpoTranscurrido(){
    	
    	String timeQuery = "tiempo(T," + this.getSituation() + ")";
        Hashtable[] result = this.query(timeQuery);
        
        if(result!=null){
        int tiempo = Integer.parseInt(result[0].get("T").toString());
        return tiempo;
        }
        System.out.println("result=null en EstadoAgente - gettpoTranscurrido");
        return 0;
        
     }
    	public int getTpoTotal(){
    	
    	String timeQuery = "tiempoTotal(F," + this.getSituation() + ")";
        Hashtable[] result = this.query(timeQuery);
        
        if(result!=null){
        int tiempo = Integer.parseInt(result[0].get("F").toString());
        return tiempo;
        }
        System.out.println("result=null en EstadoAgente - gettpoTranscurrido");
        return 0;
        
     }
     public void settpoTranscurrido(int arg){
        tpoTranscurrido += arg;
     }
     public int getdiasEsperando(){
        return diasEsperando;
     }
     public void setdiasEsperando(int arg){
        diasEsperando += arg;
     }
     public int getposicionAgente(){
        return posicionAgente;
     }
     public void setposicionAgente(int arg){
        posicionAgente = arg;
     }

	public String getPerceptionName() {
		return perceptionName;
	}

	public void setPerceptionName(String perceptionName) {
		this.perceptionName = perceptionName;
	}

	public int getPerceptionTime() {
		return perceptionTime;
	}

	public void setPerceptionTime(int perceptionTime) {
		this.perceptionTime = perceptionTime;
	}

	public boolean isCanceled() {
		if(perceptionName.equals("Refuse Forecast Request")){
			return true;
		}
		return false;
	}

	public int getIteraciones() {
		return iteraciones;
	}
	/**
	 * funcion que setea en cero las iteraciones si recibe un parametro
	 * que sea -1. Si recibe n�meros mayores que cero incrementa en 1
	 * las iteraciones
	 * @param iteraciones
	 */
	public void setIteraciones(int iteraciones) {
		if(iteraciones>0){
			this.iteraciones += iteraciones;
		}
		this.iteraciones = 0;
	}  
	
}
